package com.house.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.house.dto.HouseDTO;
import com.house.entity.House;
import com.house.entity.Img;
import com.house.entity.Room;
import com.house.reportisy.BillRepository;
import com.house.reportisy.HouseRepository;
import com.house.reportisy.ImgRepository;
import com.house.service.HouseService;
import com.house.vo.HouseVO;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author: GGP
 * @description: 房源service层实现类
 * @time: 2023-04-12 10:30
 **/
@Service
public class HouseServiceImpl implements HouseService {

	@Resource
	private HouseRepository houseRepository;

	@Resource
	private ImgRepository imgRepository;

	@Resource
	private BillRepository billRepository;

	@Override
	public House saveHouse(HouseDTO houseDTO) {
		House house = new House();
		BeanUtil.copyProperties(houseDTO, house, "id");
		House saveHouse = houseRepository.save(house);
		List<String> imgs = houseDTO.getImgs();
		List<Img> imgList=new ArrayList<>();
		imgs.forEach(s -> {
			Img img = new Img();
			img.setUrl(s);
			img.setHouseId(saveHouse.getId());
			imgList.add(img);
		});
		imgRepository.saveAll(imgList);
		return saveHouse;
	}

	@Override
	public HouseVO findHouseById(Integer id) {
		Map<String,Object> houses= houseRepository.findHouseById(id);
		HouseVO houseVO = new HouseVO();
		BeanUtil.copyProperties(houses,houseVO);
		List<String> imgPaths=new ArrayList<>();
		List<Img> imgs = findImgs(id);
		imgs.forEach(img -> {
			imgPaths.add(img.getUrl());
		});
		houseVO.setImgs(imgPaths);
		return houseVO;
	}

	@Override
	public List<Img> findImgs(Integer houseId) {
		Img img = new Img();
		img.setHouseId(houseId);
		return imgRepository.findAll(Example.of(img, ExampleMatcher.matching()));
	}

	@Override
	public House updateScore(Integer id) {
		Optional<House> byId = houseRepository.findById(id);
		House house = byId.orElse(null);
		House updateRoom = new House();
		BeanUtil.copyProperties(house,updateRoom);
		Map<String, Object> allScore = billRepository.findAllScore(id);
		BigDecimal totalScore = (BigDecimal) allScore.get("totalScore");
		double score = totalScore.intValue();
		BigInteger totalCount = (BigInteger) allScore.get("totalCount");
		double count= totalCount.intValue()*5;
		double mark= ((score/count)*10)/ 2;
		int marks= (int) mark;
		updateRoom.setScore(marks);
		return houseRepository.save(updateRoom);
	}


	@Override
	public Page<Map<String, Object>> findAllHouse(HouseDTO houseDTO) {
		Integer page = houseDTO.getPage();
		page = page==null||page<0? 0: page-1;
		Pageable pageable = houseDTO.getPublisherId()==null
				? PageRequest.of(page, houseDTO.getSize(), Sort.Direction.DESC, "score")
				: PageRequest.of(page, houseDTO.getSize(), Sort.Direction.DESC, "publish_time");
		return houseRepository.findAllHouse(houseDTO,pageable);
	}

}
